{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "1. 图像处理\n",
    "1.1 内容描述\n",
    "将三通道图片转换为灰度图。\n",
    "1.2 代码编写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#转换为灰度图\n",
    "def convert2gray(img):\n",
    "    if len(img.shape) > 2:\n",
    "        gray = np.mean(img, -1)\n",
    "        # 上面的转法较快，正规转法如下\n",
    "        # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]\n",
    "        # gray = 0.2989 * r + 0.5870 * g + 0.1140 * b\n",
    "        return gray\n",
    "    else:\n",
    "        return img"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "2. 文字与向量互相转换\n",
    "2.1 内容描述\n",
    "将输入的文本转换为one-hot形式向量，以及将ids化的向量转换为原始文本。\n",
    "2.2 代码编写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#根据字母获取ascii码，并根据ascii码转换下标\n",
    "def char2pos(c):\n",
    "    if c =='_':\n",
    "        k = 62\n",
    "        return k\n",
    "    k = ord(c)-48\n",
    "    if k > 9:\n",
    "        k = ord(c) - 55\n",
    "        if k > 35:\n",
    "            k = ord(c) - 61\n",
    "            if k > 61:\n",
    "                raise ValueError('No Map')\n",
    "    return k\n",
    "#文本转换为向量\n",
    "def text2vec(text):\n",
    "    text_len = len(text)\n",
    "    if text_len > MAX_CAPTCHA:\n",
    "        raise ValueError('验证码最长4个字符')\n",
    "    #生成MAX_CAPTCHA * CHAR_SET_LEN的都是0的矩阵\n",
    "    vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN)\n",
    "    for i, c in enumerate(text):\n",
    "        idx = i * CHAR_SET_LEN + int(char2pos(c))\n",
    "        vector[idx] = 1\n",
    "    return vector\n",
    "# 向量转回文本\n",
    "def vec2text(vec):\n",
    "    text=[]\n",
    "    for i, c in enumerate(vec):\n",
    "        #char_at_pos = i #c/63\n",
    "        char_idx = c % CHAR_SET_LEN\n",
    "        if char_idx < 10:\n",
    "            char_code = char_idx + ord('0')\n",
    "        elif char_idx <36:\n",
    "            char_code = char_idx - 10 + ord('A')\n",
    "        elif char_idx < 62:\n",
    "            char_code = char_idx-  36 + ord('a')\n",
    "        elif char_idx == 62:\n",
    "            char_code = ord('_')\n",
    "        else:\n",
    "            raise ValueError('error')\n",
    "        text.append(chr(char_code))\n",
    "    return ''.join(text)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "3. 生成batch数据\n",
    "3.1 内容描\n",
    "根据传入的batch_size参数，构建批量数据。\n",
    "3.2 代码编写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成图像及对应文本，并将大小不是(60, 160, 3)进行重新生成\n",
    "def wrap_gen_captcha_text_and_image():\n",
    "    while True:\n",
    "        text, image = gen_captcha_text_and_image()\n",
    "        if image.shape == (60, 160, 3):\n",
    "            return text, image\n",
    "# 生成一个训练batch数据\n",
    "def get_next_batch(batch_size=128):\n",
    "    batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH])\n",
    "    batch_y = np.zeros([batch_size, MAX_CAPTCHA * CHAR_SET_LEN])\n",
    "    for i in range(batch_size):\n",
    "        text, image = wrap_gen_captcha_text_and_image()\n",
    "        image = convert2gray(image)\n",
    "        batch_x[i, :] = image.flatten() / 255  # (image.flatten()-128)/128  mean为0\n",
    "        batch_y[i, :] = text2vec(text)\n",
    "    return batch_x, batch_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
